package exercise;

import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

import static java.lang.String.copyValueOf;

public class Main {
    static UserList list=null;
    public static void main(String[] args) {
        list=new UserList();
        User newUser;
        while (true){
            do{
                newUser=new User();
            }while (!newUser.valid);
            list.add(newUser);
        }
    }
}
class UserList{
    User start;
    User last;
    UserList(){
        do{
            this.start=new User();
        }while (!this.start.valid);
        this.last=this.start;
    }
    void add(User newUser){
        User node=this.start;
        while (node.next!=null){
            node=node.next;
        }
        node.next=newUser;
        this.last=newUser;
    }
    void show(){
        System.out.println("已注册用户信息：");
        for (User node=this.start; node!=null;node=node.next){
            System.out.println(node);
        }
    }
}
class User{
    static final String EXIT_USERNAME="0000";
    private static final int NAME=0,CAPTCHA=1,EMAIL=2;
    private static final String eMailRex=
            "\\w+@\\w+\\.\\w{2,3}(\\.\\w{2,3})*";
    private String name;
    private String eMail;
    private int age;
    public User next;
    boolean valid;
    public static int nodeCount=0;
    public static boolean again=false;
    User(){
        Captcha captcha=new Captcha();
        Scanner scanner=new Scanner(System.in);
        String[] info=new String[3];

        this.valid=true;
        /*read input*/
        if(again)
            System.out.println("请再试一次");
        else
            System.out.println("注册\n");
        System.out.println("请输入注册信息： 用户名 验证码"
                +"（下列字符中第_" +(1+captcha.getStart())+"_个开始数_"
                +Captcha.CAPTCHA_LENGTH+
                "_位） 邮箱 年龄");
        System.out.println(captcha.getText());
        System.out.println("您也可以直接输入0000退出");
        //name
        info[NAME]=scanner.next();
        if(info[NAME].equals(EXIT_USERNAME)){
            if (Main.list==null){
                System.out.println("暂无用户注册");
            }
            else {
                Main.list.show();
            }
            System.out.println("欢迎再次使用！");
            System.exit(0);
        }
        else {
            name = info[NAME];
        }
        info[CAPTCHA]=scanner.next();
        if(!captcha.verify(info[CAPTCHA])){
            System.out.println("验证码输入错误");
            this.valid=false;
        }
        info[EMAIL]=scanner.next();
        if(info[EMAIL].matches(eMailRex)){
            this.eMail=info[EMAIL];
        }
        else{
            System.out.println("无效邮箱地址");
            this.valid=false;
        }

        try {
            age=scanner.nextInt();
        }
        catch (InputMismatchException ime){
            System.out.println("输入的年龄不是数字");
            this.valid=false;
        }
    }

    @Override
    public String toString() {
        return "\t姓名："+this.name
                +"\t邮箱："+this.eMail
                +"\t年龄："+this.age;
    }

}
class Captcha{
    static final int CAPTCHA_LENGTH=4,CAPTCHA_SELECT_LENGTH=10;
    static final int ASCII_a=97,ALPHABET=26;

    public int getStart() {
        return start;
    }

    public String getText() {
        return text;
    }

    private final int start;
    private final String text,key;
    Captcha() {
        Random random=new Random();
        char[] captchaCandidate = new char[CAPTCHA_SELECT_LENGTH];
        char fill;
        int ascii;
        for (int index = 0; index < CAPTCHA_SELECT_LENGTH; index++) {
            ascii= ((int)(Math.random() * ALPHABET)+ ASCII_a);
            fill=(char)ascii;
            captchaCandidate[index] = fill;
        }
        this.start = (int) (
                Math.random() *
                        (CAPTCHA_SELECT_LENGTH - CAPTCHA_LENGTH)
        );
        this.text = String.copyValueOf(captchaCandidate);
        this.key = text.substring(start, start + CAPTCHA_LENGTH);
    }
    boolean verify(String trial){
        return trial.equals(this.key);
    }
}